<?php
/**
 * 作者: https://gitee.com/miren123
 * 时间: 2022-05-24 11:13:53
 * 描述: 个人中心
 */

namespace app\admin\controller;

use think\facade\Request;
use app\common\validate\AdminUserCenterValidate;
use app\common\service\AdminUserCenterService;
use app\common\service\AdminMenuService;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("个人中心")
 * @Apidoc\Group("adminCenter")
 * @Apidoc\Sort("50")
 */
class AdminUserCenter
{
    /**
     * @Apidoc\Title("我的信息")
     * @Apidoc\Header(ref="headerAdmin")
     * @Apidoc\Returned(ref="app\common\model\AdminUserModel\info")
     */
    public function info()
    {
        $param['admin_user_id'] = admin_user_id();

        validate(AdminUserCenterValidate::class)->scene('info')->check($param);

        $data = AdminUserCenterService::info($param['admin_user_id']);

        if ($data['is_delete'] == 1) {
            exception('账号已被删除！');
        }

        return success($data);
    }

    /**
     * @Apidoc\Title("可用菜单")
     * @Apidoc\Header(ref="headerAdmin")
     * @Apidoc\Returned(ref="returnPaging"),
     * @Apidoc\Returned("list", type="array", desc="树形列表",
     *     @Apidoc\Returned(ref="app\common\model\AdminMenuModel\list")
     * )
     */
    public function menu()
    {

        $param['admin_user_id'] = admin_user_id();

        validate(AdminUserCenterValidate::class)->scene('menu')->check($param);

        $data = AdminUserCenterService::menu($param['admin_user_id']);

        return success($data);
    }

    /**
     * @Apidoc\Title("修改信息")
     * @Apidoc\Method("POST")
     * @Apidoc\Header(ref="headerAdmin")
     * @Apidoc\Param(ref="app\common\model\AdminUserModel\edit")
     */
    public function edit()
    {
        $param['admin_user_id'] = admin_user_id();
        $param['username']      = Request::param('username/s', '');
        $param['nickname']      = Request::param('nickname/s', '');
        $param['phone']         = Request::param('phone/s', '');
        $param['email']         = Request::param('email/s', '');
        $param['desc']         = Request::param('desc/s', '');

        validate(AdminUserCenterValidate::class)->scene('edit')->check($param);

        $data = AdminUserCenterService::edit($param);

        return success($data);
    }

    /**
     * @Apidoc\Title("修改密码")
     * @Apidoc\Method("POST")
     * @Apidoc\Header(ref="headerAdmin")
     * @Apidoc\Param(ref="app\common\model\AdminUserModel\id")
     * @Apidoc\Param("password_old", type="string", require=true, desc="原密码")
     * @Apidoc\Param("password_new", type="string", require=true, desc="新密码")
     */
    public function pwd()
    {
        $param['admin_user_id'] = admin_user_id();
        $param['password_old']  = Request::param('password_old/s', '');
        $param['password_new']  = Request::param('password_new/s', '');

        validate(AdminUserCenterValidate::class)->scene('pwd')->check($param);

        $data = AdminUserCenterService::pwd($param);

        return success($data);
    }

    /**
     * @Apidoc\Title("更换头像")
     * @Apidoc\Method("POST")
     * @Apidoc\Header(ref="headerAdmin")
     * @Apidoc\ParamType("formdata")
     * @Apidoc\Param(ref="app\common\model\AdminUserModel\avatar")
     * @Apidoc\Returned("admin_user_id", type="string",desc="用户id")
     * @Apidoc\Returned("avatar", type="string",desc="头像地址")
     * @Apidoc\Returned("update_time", type="string",desc="修改时间")
     */
    public function avatar()
    {
        $param['admin_user_id'] = admin_user_id();
        $param['avatar']        = Request::file('avatar_file');

        validate(AdminUserCenterValidate::class)->scene('avatar')->check($param);

        $data = AdminUserCenterService::avatar($param);

        return success($data);
    }

}
